#!/bin/sh

# Slackware build script for Icecat

# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016  Patrick J. Volkerding, Sebeka, Minnesota, USA
# Copyright 2017 Azure Zanculmarktum <zanculmarktum@gmail.com>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Almost all of the code was taken from slackware-14.2/patches/source/mozilla-firefox/

PRGNAM=Icecat
VERSION=${VERSION:-52.3.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCNAM=icecat

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
  OPTIMIZE=" --enable-optimize=-O1 "
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  OPTIMIZE=" --enable-optimize=-O1 "
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  OPTIMIZE=" --enable-optimize=-O1 "
elif [ "$ARCH" = "arm" ]; then
  SLKCFLAGS="-O2 -march=armv4 -mtune=xscale"
  LIBDIRSUFFIX=""
  OPTIMIZE=" --enable-optimize=-O1 "
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  OPTIMIZE=" --enable-optimize=-O1 "
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

# Build and install autoconf-2.13 (like PaleMoon)
cd $TMP
rm -rf autoconf-2.13
tar xvf $CWD/autoconf-2.13.tar.xz
cd autoconf-2.13
zcat $CWD/autoconf-2.13-consolidated_fixes-1.patch.gz | patch -p1 --verbose
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

rm -rf $TMP/autoconf-tmp
mkdir -p $TMP/autoconf-tmp
chmod 700 $TMP/autoconf-tmp
rm -rf $TMP/autoconf-tmp/*
mkdir -p $TMP/autoconf-tmp/usr

./configure \
  --prefix=$TMP/autoconf-tmp/usr \
  --program-suffix=-2.13 \
  --infodir=$TMP/autoconf-tmp/usr/info \
  --mandir=$TMP/autoconf-tmp/usr/man \
  --build=$ARCH-slackware-linux

make || make -j1
make install
PATH=$TMP/autoconf-tmp/usr/bin:$PATH

# Build Icecat
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION-gnu1.tar.bz2
cd $SRCNAM-$VERSION
chown -R root:root .
find -L . \
  \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
   -o -perm 511 \) -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
   -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Without LANG=C, building the Python environment may fail with:
# "UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 36: ordinal not in range(128)"
export LANG=C

# Link using gold.  This avoids running out of memory on 32-bit systems, and
# avoids a recurring build failure with GNU ld on other systems.
chmod 755 $CWD/gold
export CC="gcc -B$CWD/gold"
export CXX="g++ -B$CWD/gold"
export LD="$CWD/gold"

OPTIONS="\
  --enable-official-branding \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --with-default-mozilla-five-home=/usr/lib${LIBDIRSUFFIX}/icecat-$VERSION \
  --with-system-zlib \
  --enable-alsa \
  --enable-application=browser \
  --enable-default-toolkit=cairo-gtk2 \
  --enable-startup-notification \
  $OPTIMIZE \
  --enable-strip \
  --enable-cpp-rtti \
  --disable-accessibility \
  --disable-crashreporter \
  --disable-debug"

export MOZILLA_OFFICIAL="1"
export BUILD_OFFICIAL="1"
export MOZ_PHOENIX="1"
export MOZ_PACKAGE_JSSHELL="1"
export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"

# Clear some variables that could break the build
unset DBUS_SESSION_BUS_ADDRESS ORBIT_SOCKETDIR SESSION_MANAGER \
  XDG_SESSION_COOKIE XAUTHORITY MAKEFLAGS

# Assemble our .mozconfig
echo ". \$topsrcdir/browser/config/mozconfig" > .mozconfig

# Write in it the options above
for option in $OPTIONS; do echo "ac_add_options $option" >> .mozconfig; done

echo "ac_add_options --disable-tests" >> .mozconfig
make -f client.mk build

make -f client.mk install DESTDIR=$PKG

# Removes unneeded files
rm -rf $PKG/usr/lib${LIBDIRSUFFIX}/icecat-devel-$VERSION
rm -rf $PKG/usr/include

mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

# Need some default icons in the right place:
for i in 16 22 24 32 48 256; do
  install -D -m 0644 browser/branding/official/default${i}.png \
    $PKG/usr/share/icons/hicolor/${i}x${i}/apps/icecat.png
done
mkdir -p $PKG/usr/share/pixmaps
( cd $PKG/usr/share/pixmaps ; ln -sf $PKG/usr/share/icons/hicolor/256x256/apps/icecat.png . )
install -D -m 644 browser/branding/official/default16.png \
  $PKG/usr/lib$LIBDIRSUFFIX/icecat-$VERSION/icons/default16.png
install -D -m 644 browser/branding/official/default16.png \
  $PKG/usr/lib$LIBDIRSUFFIX/icecat-$VERSION/chrome/icons/default/default16.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS CLOBBER Changelog.IceCat LEGAL LICENSE README.IceCat README.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
